<?php
	include("../session_ini.php");

	include_once("../model/utils/Thumb.php");
	include_once("../model/UsuarioService.php");

	/**
	 * Jcrop image cropping plugin for jQuery
	 * Example cropping script
	 * @copyright 2008 Kelly Hallman
	 * More info: http://deepliquid.com/content/Jcrop_Implementation_Theory.html
	 */
	$targ_w = 72;
	$targ_h = 87;
	$jpeg_quality = 90;

	$src = str_replace("http://".$_SERVER['HTTP_HOST'], "",$_POST["src"]);
	$src = str_replace(URL_BASE(), "..", $src);
	
	/*Imagen de perfil*/
	
	$mythumb = new Thumb();  
	$mythumb->loadImage($src);  
	
	$medidas = array($_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']);
	
	$mythumb->crop($targ_w, $targ_h, $medidas);
	
	$directorio = md5(mt_rand(00000000, 99999999)).'.jpg';
	$directorio2 = "../view/uploads/avatars/".$directorio;
	
	$mythumb->save($directorio2, $jpeg_quality);
	
	$service = new UsuarioService();
	
	$service->cambiarAvatar($_SESSION["id_usuario"], $directorio);
	$_SESSION["imagen"] = $directorio;
	
	echo URL_BASE()."/view/uploads/avatars/".$directorio;
	
	//unlink($src);
?>
